package cn.ac.iie.main;

import cn.ac.iie.util.RedisUtil;
import java.util.concurrent.TimeUnit;
import org.redisson.RedissonClient;
import org.redisson.core.RLock;

/**
 *
 * @author zmc
 */
public class TestLockThread extends Thread {

    RedissonClient redisson;
    RedisUtil util;

    public TestLockThread(
            RedissonClient redisson,
            RedisUtil util) {
        this.redisson = redisson;
        this.util = util;

    }

    @Override
    public void run() {
        String key = "1111";
        RLock lock = redisson.getLock("test");
        lock.lock(1, TimeUnit.SECONDS);
        try {
            String valueStr = util.get(key);
            if (valueStr == null || valueStr.isEmpty()) {
                valueStr = "0";
            }
            System.out.println("Value:" + valueStr);
            int value = Integer.valueOf(valueStr) + 1;
            util.set(key, Integer.toString(value));
        } finally {
            lock.unlock();
        }
    }
}
